#!/bin/bash
# Begin ner.server
##############
#$# creator : Devon Smith
#%# email: smithde@oclc.org
#$# created : 2010-06-18
#$# title : ner.server
#$# description : Start a NER server
########## Define a usage function 
function usage {
    echo "Usage: $this"
    if [ "$1" = "exit" ]; then exit; fi
}
########## Set to "false" to turn verbose off
function echo_stdout { echo "$@"; }
function echo_stderr { echo "$@" >& 2; }
# Set verbose to 'echo_stderr' or 'echo_stdout' to see output trace info
verbose=echo_stdout
$verbose Verbose output on ${verbose##*_}

########## Setup some common paths
# The absolute, canonical ( no ".." ) path to this script
canonical=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")")
# Just the filename of this script
this=$(basename $canonical)
$verbose This: $this
# The directory this script is in
here=$(dirname $canonical)
$verbose Here: $here

########## When the script is being executed
today=$(date +%F)
now=$(date +%H:%M:%S)
$verbose Time: $today $now
##########
if [ "$1" = "-h" ]; then usage 'exit' ; fi
##########
JAVA=$(which java)

moduledir=$(dirname $(dirname $here))
$verbose Module Dir: $moduledir

configdir=$moduledir/src/config
$verbose Config Dir: $configdir

libdir=$(dirname $moduledir)/lib
$verbose Library Dir: $libdir
jars=(uiuc.ner.jar uiuc.ner.model.jar LBJ2.jar LBJ2Library.jar)

restlet_libdir=$libdir/restlet-1.0.11/lib
$verbose Restlet Library Dir: $restlet_libdir
restlet_jars=(org.restlet.jar com.noelios.restlet.jar com.noelios.restlet.ext.simple_3.1.jar)

simple_libdir=$libdir/simple-core-3.1.3/jar
$verbose Simple Core Library Dir: $simple_libdir
simple_jars=(simple-core-3.1.3.jar)

freemarker_libdir=$libdir/freemarker-2.3.19/lib
$verbose FreeMarker Library Dir: $freemarker_libdir
freemarker_jars=(freemarker.jar)

if [ ! -d $libdir ]; then echo "EMP-Suite JARs not found: '$libdir'"; exit; fi
if [ ! -d $restlet_libdir ]; then echo "Restlet JARs not found: '$restlet_libdir'"; exit; fi
if [ ! -d $simple_libdir ]; then echo "Simple Core JARs not found: '$simple_libdir'"; exit; fi
if [ ! -d $freemarker_libdir ]; then echo "FreeMaker JARs not found: '$freemarker_libdir'"; exit; fi

DEST=$moduledir/target/classes
CP=$DEST
for jar in ${jars[*]}; do CP=${CP:+$CP:}$libdir/$jar; done
for jar in ${restlet_jars[*]}; do CP=${CP:+$CP:}$restlet_libdir/$jar; done
for jar in ${simple_jars[*]}; do CP=${CP:+$CP:}$simple_libdir/$jar; done
for jar in ${freemarker_jars[*]}; do CP=${CP:+$CP:}$freemarker_libdir/$jar; done
$verbose Classpath: $CP

CLASS=org.oclc.gateman.GatemanServer
LOGGING="-Djava.util.logging.config.file=$configdir/logging.properties"
mkdir -p logs
$JAVA -Xmx2000m $LOGGING -cp $CP $CLASS

############
# End ner.server
# vim:ts=4:indentexpr=
